﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BigDataFramework.Library.Objects.DataAdapters;
using BigDataFramework.Library.Objects.DataAdapters.Twitter;
using BigData.DataContracts.Objects.Twitter;
using BigDataFramework.Library.Objects.DataAdapters.Reddit;
using BigData.DataContracts.Objects.FourSquare;
using BigDataFramework.Library.Objects.DataAdapters.FourSquare;
using BigData.DataContracts.Objects.BigData;
using BigDataFramework.Repositories;

namespace BigDataFramework.Library.Objects
{
    public class APIDataRetriever
    {
        public void RetrieveData()
        {
            // How to pull data for a specific person
            MongoDBRepository dbContext;
            BigDataUser user;

            dbContext = new MongoDBRepository();

            // Pulling Gilligan's user data
            user = dbContext.ReadOne<BigDataUser>(bdu =>
                        bdu.FirstName == "Gilligan"
                    && bdu.LastName == "Willoughby"

                    && bdu.RecordStartDate <= DateTime.Now
                    && bdu.RecordEndDate >= DateTime.Now
                );

            // Foursquare: Checkins
            DataAdapterBase<BigDataFourSquareCheckin> dataAdapter = new FourSquareDataAdapterCheckin();
            List<BigDataFourSquareCheckin> checkins;
            // Retrieving his checkins from the dataAdapter... which in turn pulls everything from the foursquare API
            checkins = dataAdapter.GetDataUpdatesSinceLastSync(
                        user: user
                    , shouldUpdateLastSyncDate: true
                );

            if (checkins.Count > 0)
                dbContext.Create(checkins);

            // REDDIT: Comments
            CommentsAdapter redditcommentAdapter = new CommentsAdapter();
            List<BigData.DataContracts.Objects.Reddit.Comment> comments = redditcommentAdapter.GetDataUpdatesSinceLastSync(
                        user: user
                    , shouldUpdateLastSyncDate: true
                );

            if (comments.Count > 0)
                dbContext.Create(comments);

            // Twitter
            DataAdapterBase<BigDataTwitterTweet> twitterAdapter = new TwitterDataAdapter();
            List<BigDataTwitterTweet> tweets;
            // Retrieving his checkins from the dataAdapter... which in turn pulls everything from the foursquare API
            tweets = twitterAdapter.GetDataUpdatesSinceLastSync(
                        user: user
                    , shouldUpdateLastSyncDate: true
                );

            if (tweets.Count > 0)
                dbContext.Create(tweets);
        }
    }
}
